FROM php:{{ php_version }}-cli-alpine{{ alpine_version }}

COPY --from=composer:{{ composer.version }} /usr/bin/composer /usr/bin/

RUN \
    set -ex && \
    apk update && \
    apk add --no-cache libstdc++ libpq && \
    apk add --no-cache --virtual .build-deps $PHPIZE_DEPS {% if option_curl == true  %}curl-dev{% endif %} {% if php_version|slice(0, 2) != '7.' and php_version|slice(0, 3) not in ['8.0', '8.1'] %}linux-headers {% endif %}brotli-dev postgresql-dev openssl-dev pcre-dev pcre2-dev sqlite-dev zlib-dev && \
# PHP extension pdo_mysql is included since 4.8.12+ and 5.0.1+.
    docker-php-ext-install pdo_mysql && \
{%~ if php_extensions is not empty %}
    pecl channel-update pecl.php.net && \
    {%~ for name, data in php_extensions %}
        {%~ if data.configureoptions is empty %}
    pecl install {{ name }}{% if data.version is not empty %}-{{ data.version }}{% endif %} && \
        {%~ else %}
    pecl install --configureoptions '{{ data.configureoptions }}' {{ name }}{% if data.version is not empty %}-{{ data.version }}{% endif %} && \
        {%~ endif %}
    {%~ endfor %}
# PHP extension Redis is included since 4.8.12+ and 5.0.1+.
    {%~ for name, data in php_extensions %}
        {%~ if data.enabled %}
    docker-php-ext-enable {{ name }} && \
        {%~ endif %}
    {%~ endfor %}
{%~ endif %}
    docker-php-ext-install sockets && \
    docker-php-source extract && \
    mkdir /usr/src/php/ext/swoole && \
    curl -sfL https://github.com/swoole/swoole-src/archive/{% if swoole_version == "nightly" %}master{% else %}v{{ swoole_version }}{% endif %}.tar.gz -o swoole.tar.gz && \
    tar xfz swoole.tar.gz --strip-components=1 -C /usr/src/php/ext/swoole && \
    docker-php-ext-configure swoole \
{% if option_curl == true %}{{ "        --enable-swoole-curl   \\\n" }}{% endif %}
{% if option_json == true %}{{ "        --enable-swoole-json   \\\n" }}{% endif %}
        --enable-mysqlnd       \
        --enable-swoole-pgsql  \
        --enable-swoole-sqlite \
        --enable-brotli        \
        --enable-openssl       \
        --enable-sockets    && \
    docker-php-ext-install -j$(nproc) swoole && \
    rm -f swoole.tar.gz && \
    docker-php-source delete && \
    apk del .build-deps

WORKDIR "/var/www/"
